(* e81b - For noForth c&v FR59xx vsn 200202ff, Battery monitoring example.
   Switching to the lowest possible power state in 5 to 6 steps. Showing 
   the result of each step!
   The remaining power consumption is largely due to the LDO stabilizer used.
   This example is run on the Micro Launchpad FR5 with the MSP430FR5949
   as MPU and the MCP1703T3302E a 3V3 low current LDO power supply.

   ADC on, sample time at 64 clocks

   Lower voltages could use 2.5 Volt reference for a higher resolution
   REFCTL0 = 1B0 = 31 selects 2.5 Volt reference, 1 selects 1.2 Volt reference.

   ADC-clock/4, sample timer on, 12-bit resolution
    P1.0 = A0   = 00
    P1.1 = A1   = 01
    P1.2 = A2   = 02
    P1.4 = A4   = 04
    P1.5 = A5   = 05
    P3.0 = A12  = 0C
    P3.1 = A13  = 0D
    P3.2 = A14  = 0E

  12-bit ADC using internal ADC12 of MSP430FR5xxx
  Use P3.0 as analog input and P1.2 to P1.7 as LED output

\ Example routine for LiPo protection
: LIPO-EMPTY  ( -- )        \ Short flashing of led when accu is empty
    @accu dm 310 < if       \ Accu almost empty (lower then 3V1)?
        lp                  \ Switch target to low energy usage
        begin
            FF >leds  5 ms  \ Flash led shortly
            EF >leds  800 ms
        again               \ Endlessly
    then ;

0) 8.3000 mA  When all leds are on and at full speed (16MHz)
1) 1,4700 mA  Full speed all leds off  - 0 >LEDS
2) 0,5600 mA  SMCLK from 16MHz to 1MHz - 1MHZ
3) 0,1800 mA  With potentiometer off   - POT-OFF 
4) 0,1700 mA  With ADC off             - ADC-OFF
5) 0,1500 mA  Defined I/O pin state    - SET-PINS
6) 0,1400 mA  When VREF is off         - REF-OFF
7) 0,0026 mA  When in LPM3             - LPM3
or 0,0025 mA  When in LPM4             - LPM4

*)

v: extra definitions

hex
\ Set DCO to 1 MHz, current use of CPU on, power-off state AM~0,4mA
code 1MHZ       ( -- )
    #8 sr bic               \ Interrupts off
    A5 # 161 & .b mov       \ CSCTL0h Open DCO change
    0040 # 162 & mov        \ CSCTL1  DCO = 1MHz
    #0 161 & .b mov         \ CSCTL0h Freeze DCO
\ Configure eUSCI A1 as UART
    0081 # 5C0 & mov        \ UCA0CTLW0  Select clock source, USCI off
    DM 08 # 5C6 & mov       \ UCA1BRW    Set baudrate 115K2 at 1MHz
    D600 # 5C8 & mov        \ UCA1CTLW   Set modulation control
    #1 5C0 & bic            \ USCI active
    dm 249 # adr ms# & mov  \ MS timing for 1 MHz DCO
    next
end-code

code LPM3       D0 # sr bis  next  end-code \ Go from AM to LPM3
code LPM4       F0 # sr bis  next  end-code \ Go from AM to LPM4


(* Note that, the setting of the I/O-pins is very much
   dependant of the connected devices! 
   In this example it are some LEDs and a potentiometer.
   P1.2 to P1.7 are 6 LEDs, PJ.1 is a single LED
   and P1.0 switches the a potentiometer on or off.
*)

: SET-PINS  ( -- )      \ All other I/O pins to a defined state!
    -1 204 c!   -1 205 c!
    -1 224 c!   -1 225 c!
    2 322 *bic  -1 324 c!  ;

: BATTERY1  ( -- )      \ Step by step to low power mode 3
    adc-on  base @  decimal  pot-on
    begin
        0C adc  dup .  dup >leds  80 ms
        dm 3390 < if
            cr ." To low power: "
            0 >leds  ch 1 emit  key drop
            1mhz     ch 2 emit  key drop
            pot-off  ch 3 emit  key drop
            adc-off  ch 4 emit  key drop
            set-pins ch 5 emit  key drop
            ref-off  ch 6 emit  key drop
            ."  LPM3 "   lpm3
        then
    key? until  base !  0  >leds ;

: BATTERY2   ( -- )      \ Step by step to low power mode 4
    adc-on  base @  decimal  pot-on
    begin
        0C adc  dup .  dup >leds  80 ms
        dm 3390 < if
            cr ." To low power: "
            0 >leds  ch 1 emit  key drop
            1mhz     ch 2 emit  key drop
            pot-off  ch 3 emit  key drop
            adc-off  ch 4 emit  key drop
            set-pins ch 5 emit  key drop
            ref-off  ch 6 emit  key drop
            ."  LPM4 "   lpm4
        then
    key? until  base !  0 >leds ;

: BATTERY3   ( -- )      \ Step by step to low power mode 4 without the 1 MHz step!
    adc-on  base @  decimal  pot-on
    begin
        0C adc  dup .  dup >leds  80 ms
        dm 3390 < if
            cr ." To low power: "
            0 >leds  ch 1 emit  key drop
            pot-off  ch 2 emit  key drop
            adc-off  ch 3 emit  key drop
            set-pins ch 4 emit  key drop
            ref-off  ch 5 emit  key drop
            ."  LPM4 "   lpm4
        then
    key? until  base !  0 >leds ;


v: fresh
' battery1  to app
shield BATTERY\  freeze

\ End ;;;

